package com.cykj.pos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cykj.common.annotation.DataSource;
import com.cykj.common.enums.DataSourceType;
import com.cykj.pos.domain.BizMerchBill;
import com.cykj.pos.domain.BizShareDetail;
import com.cykj.pos.domain.dto.BillQueryDTO;
import com.cykj.pos.domain.dto.MsgMerchBillDTO;
import com.cykj.pos.mapper.BizMerchBillMapper;
import com.cykj.pos.profit.dto.BizCashoutRecordsDTO;
import com.cykj.pos.profit.dto.MessageDTO;
import com.cykj.pos.profit.dto.ShareDetailDTO;
import com.cykj.pos.service.IBizMerchBillService;
import com.cykj.pos.service.IBizShareDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 账单信息Service业务层处理
 *
 * @author weijianbo
 * @date 2021-02-06
 */
@Service
public class BizMerchBillServiceImpl extends ServiceImpl<BizMerchBillMapper, BizMerchBill> implements IBizMerchBillService {
    @Autowired
    BizMerchBillMapper merchBillMapper;

    @Autowired
    IBizShareDetailService bizShareDetailService;

    @Override
    public List<BizMerchBill> queryList(BizMerchBill bizMerchBill) {
        LambdaQueryWrapper<BizMerchBill> lqw = Wrappers.lambdaQuery();
        if (bizMerchBill.getMerchId() != null){
            lqw.eq(BizMerchBill::getMerchId ,bizMerchBill.getMerchId());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getMerchName())){
            lqw.like(BizMerchBill::getMerchName ,bizMerchBill.getMerchName());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getPosCode())){
            lqw.eq(BizMerchBill::getPosCode ,bizMerchBill.getPosCode());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getPosType())){
            lqw.eq(BizMerchBill::getPosType ,bizMerchBill.getPosType());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getBillType())){
            lqw.eq(BizMerchBill::getBillType ,bizMerchBill.getBillType());
        }
        if (bizMerchBill.getAmount() != null){
            lqw.eq(BizMerchBill::getAmount ,bizMerchBill.getAmount());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getPolicyId())){
            lqw.eq(BizMerchBill::getPolicyId ,bizMerchBill.getPolicyId());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getBillDate())){
            lqw.like(BizMerchBill::getBillDate ,bizMerchBill.getBillDate());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getVar1())){
            lqw.eq(BizMerchBill::getVar1 ,bizMerchBill.getVar1());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getVar2())){
            lqw.eq(BizMerchBill::getVar2 ,bizMerchBill.getVar2());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getVar3())){
            lqw.eq(BizMerchBill::getVar3 ,bizMerchBill.getVar3());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getVar4())){
            lqw.eq(BizMerchBill::getVar4 ,bizMerchBill.getVar4());
        }
        if (StringUtils.isNotBlank(bizMerchBill.getVar5())){
            lqw.eq(BizMerchBill::getVar5 ,bizMerchBill.getVar5());
        }
        return this.list(lqw);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<BillQueryDTO> getPageBillListByMerchId(BillQueryDTO billQueryDTO) {
        // 设置数据从第几条开始
        long start = (billQueryDTO.getPageNo() -1) * billQueryDTO.getPageSize();
        billQueryDTO.setStart(start);
        return merchBillMapper.getPageBillListByMerchId(billQueryDTO);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BigDecimal queryAwardAmount(ShareDetailDTO shareDetailDTO) {
        BigDecimal awardAmount = merchBillMapper.queryAwardAmount(shareDetailDTO);
        return awardAmount;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizMerchBill queryBillTypeOne(BizMerchBill bizMerchBill) {
        LambdaQueryWrapper<BizMerchBill> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BizMerchBill::getBillId,bizMerchBill.getBillId());
        BizMerchBill bizMerchBill1 = this.getOne(wrapper);
        return bizMerchBill1;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizCashoutRecordsDTO queryBillTypeFour(BizMerchBill bizMerchBill) {
        BizCashoutRecordsDTO bizCashoutRecords = merchBillMapper.queryBillTypeFour(bizMerchBill);
        return bizCashoutRecords;
    }
    @Override
    public List<BizMerchBill> WithdrawalList(BizMerchBill bizMerchBill) {
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        if (bizMerchBill.getBillDate()==null){
            bizMerchBill.setBillDate(format.format(date));
        }
        List<BizMerchBill> bizMerchBill1 = merchBillMapper.WithdrawalList(bizMerchBill);
        return bizMerchBill1;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public BizShareDetail queryBillTypeTwo(BizMerchBill bizMerchBill) {
        BizShareDetail bizShareDetail = bizShareDetailService.queryBillTypeTwo(bizMerchBill);
        BigDecimal decimal = new BigDecimal("100");
        BigDecimal dealShareRatio = bizShareDetail.getDealShareRatio();
        BigDecimal calcDealShareRatio = dealShareRatio.multiply(decimal).stripTrailingZeros();
        bizShareDetail.setDealShareRatio(calcDealShareRatio);
        BigDecimal crownRatio = bizShareDetail.getCrownRatio();
        BigDecimal calcCrownRatio = crownRatio.multiply(decimal).stripTrailingZeros();
        bizShareDetail.setCrownRatio(calcCrownRatio);
        return bizShareDetail;
    }

    @Override
    public MsgMerchBillDTO getMerchBillByMsgId(MessageDTO messageDTO) {
        BizMerchBill merchBill = merchBillMapper.selectMerchBillByMsgId(messageDTO);
        MsgMerchBillDTO msgMerchBillDTO = new MsgMerchBillDTO();
        if(merchBill!=null){
            msgMerchBillDTO.setAmount(merchBill.getAmount());
            msgMerchBillDTO.setPosCode(merchBill.getPosCode());
            msgMerchBillDTO.setMerchName(merchBill.getMerchName());
            msgMerchBillDTO.setBillDate(merchBill.getBillDate());
        }
        return msgMerchBillDTO;
    }

    @Override
    public void insertMerchBill(BizMerchBill merchBill) {
        merchBillMapper.insertMerchBill(merchBill);
    }

}
